package com.guotd.ticketSystem.controller;

import com.guotd.ticketSystem.entity.Flight;
import com.guotd.ticketSystem.mapper.FlightMapper;
import com.guotd.ticketSystem.utils.SessionUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.util.List;

@WebServlet("/selectFlight")
public class SelectFlightServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String fromAirport = request.getParameter("fromAirport");
        String toAirport = request.getParameter("toAirport");
        Date fromDate = Date.valueOf(request.getParameter("fromDate"));
        SqlSession session = SessionUtil.getSqlSession();
        FlightMapper flightMapper = session.getMapper(FlightMapper.class);
        List<Flight> flights = flightMapper.selectFlight(fromAirport, toAirport, fromDate);
        if (!flights.isEmpty()) {
            request.setAttribute("flights", flights);
            request.getSession().setAttribute("flight_msg", "查询成功！");
            request.getRequestDispatcher("flight_list.jsp").forward(request, response);
        } else {
            request.getSession().setAttribute("flight_msg", "未查询到符合要求的航班！");
            request.getRequestDispatcher("flight_list.jsp").forward(request, response);
        }
    }
}
